package org.example.day;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/09/12 15:43
 */
public class Solution2576 {

    public static void main(String[] args) {
        System.out.println(new Solution2576().maxNumOfMarkedIndices(new int[]{42,83,48,10,24,55,9,100,10,17,17,99,51,32,16,98,99,31,28,68,71,14,64,29,15,40}));
    }

    public int maxNumOfMarkedIndices(int[] nums) {
        if(nums.length == 1){
            return 0;
        }
        Arrays.sort(nums);
        if( 2*nums[0] > nums[nums.length - 1]){
            return 0;
        }

        int sum = 0;
        int n = nums.length;

        int j = n - 1;
        int i = n - 2;
        int lastIndex = 0;
        boolean flag;
        List<Integer> markList = new ArrayList<>();
        while (j >= 0 && i >= 0){
            flag = false;
            while ( i >= 0 && i < j){
                if(2 * nums[i] <= nums[j] && !markList.contains(i) && !markList.contains(j)){
                    flag = true;
                    markList.add(i);
                    markList.add(j);
                    System.out.println(i + "," + j);
                    i--;
                    lastIndex = i;
                    break;
                }
                i--;
            }
            j--;
            i = lastIndex;
            if(flag){
                sum+=2;
            }
        }
        return sum;
    }


    public int maxNumOfMarkedIndices2(int[] nums) {
        Arrays.sort(nums);
        int left = 0;
        int right = nums.length / 2 + 1; // 开区间
        while (left + 1 < right) {
            int mid = (left + right) >>> 1;
            if (check(nums, mid)) {
                left = mid;
            } else {
                right = mid;
            }
        }
        return left * 2; // 最多匹配 left 对，有 left * 2 个数
    }

    private boolean check(int[] nums, int k) {
        for (int i = 0; i < k; i++) {
            if (nums[i] * 2 > nums[nums.length - k + i]) {
                return false;
            }
        }
        return true;
    }


}
